﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Data;
using System.Printing;
using System.Management;
using System.IO.Ports;
using System.Windows.Controls;
using CrystalDecisions.CrystalReports.Engine;
using HMS.Data;
using HMS.Report;



namespace HMS.ClassLibrary
{
    public class Printer
    {
        private HMSEntities HMSContext;
        public Printer()
        {
            try
            {
                 HMSContext = new HMSEntities();
            }
            catch (Exception)
            {
                
                throw;
            }
         
        }

        public  string GetDefaultPrinterName()
        {
            try
            {
                LocalPrintServer printServer = new LocalPrintServer();
                PrintQueue printQueue = printServer.DefaultPrintQueue;
                return printQueue.Name;
                
            }
            catch (Exception)
            {
                
                throw;
            }
           
        }
        public bool Print(ReportClass report, Channeling channeling, int noOfCopies = 1)
        {
            ReportClass crystalReport = report;
            try
            {

                crystalReport.SetParameterValue("CompanyName", HMSContext.Settings.Where(s => s.SettingID == 1).First().Value);
                crystalReport.SetParameterValue("AddressLine1", HMSContext.Settings.Where(s => s.SettingID == 2).First().Value);
                crystalReport.SetParameterValue("AddressLine2", HMSContext.Settings.Where(s => s.SettingID == 3).First().Value);
                crystalReport.SetParameterValue("AddressLine3", HMSContext.Settings.Where(s => s.SettingID == 4).First().Value);
                crystalReport.SetParameterValue("Hotline", HMSContext.Settings.Where(s => s.SettingID == 6).First().Value);
                crystalReport.SetParameterValue("FixedLine", HMSContext.Settings.Where(s => s.SettingID == 5).First().Value);
                crystalReport.SetParameterValue("Fax", HMSContext.Settings.Where(s => s.SettingID == 8).First().Value);
                
                crystalReport.SetParameterValue("ReceiptReference",channeling.Reference);
                crystalReport.SetParameterValue("PatientName", channeling.Patient.Name);
                crystalReport.SetParameterValue("ContactNo", channeling.Patient.ContactNumber);

                crystalReport.SetParameterValue("DoctorName",channeling.Doctor.Title+ channeling.Doctor.Name);
                crystalReport.SetParameterValue("DoctorType",channeling.Doctor.DoctorType.Name);

                crystalReport.SetParameterValue("AppoinmentDate", channeling.AppointmentDateTime.Value.ToShortDateString());
                crystalReport.SetParameterValue("AppoinmentTime", channeling.AppointmentDateTime.Value.ToShortTimeString());
                crystalReport.SetParameterValue("PatientNo", channeling.PatientNumber.ToString());

                crystalReport.SetParameterValue("HospitalFee",channeling.Receipt.HospitalFee.Value.ToString());
                crystalReport.SetParameterValue("DoctorFee", channeling.Receipt.DoctorFee.Value.ToString());
                crystalReport.SetParameterValue("TotalReceiptAmount", channeling.Receipt.TotalAmount.Value.ToString());

                crystalReport.SetParameterValue("ReceiptTypeName", channeling.Receipt.ServiceType.Name);
                crystalReport.SetParameterValue("User", channeling.User.UserName);
                //crystalReport.SetParameterValue("ReportName", chanel.Doctor.name);
                return this.Print(crystalReport, noOfCopies);
               
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public bool Print(ReportClass report, Data.Reporting reporting, int noOfCopies = 1)
        {
            ReportClass crystalReport = report;
            try
            {

                crystalReport.SetParameterValue("CompanyName", HMSContext.Settings.Where(s => s.SettingID == 1).First().Value);
                crystalReport.SetParameterValue("AddressLine1", HMSContext.Settings.Where(s => s.SettingID == 2).First().Value);
                crystalReport.SetParameterValue("AddressLine2", HMSContext.Settings.Where(s => s.SettingID == 3).First().Value);
                crystalReport.SetParameterValue("AddressLine3", HMSContext.Settings.Where(s => s.SettingID == 4).First().Value);
                crystalReport.SetParameterValue("Hotline", HMSContext.Settings.Where(s => s.SettingID == 6).First().Value);
                crystalReport.SetParameterValue("FixedLine", HMSContext.Settings.Where(s => s.SettingID == 5).First().Value);
                crystalReport.SetParameterValue("Fax", HMSContext.Settings.Where(s => s.SettingID == 8).First().Value);

                crystalReport.SetParameterValue("ReceiptReference", reporting.Reference);
                crystalReport.SetParameterValue("PatientName", reporting.Patient.Name);
                crystalReport.SetParameterValue("ContactNo", reporting.Patient.ContactNumber);

                switch (reporting.Receipt.ServiceTypeID)
                {
                    case 1:
                        crystalReport.SetParameterValue("DoctorName", reporting.ReportType.Doctor.Title + reporting.ReportType.Doctor.Name);
                        crystalReport.SetParameterValue("DoctorType", reporting.ReportType.Doctor.DoctorType.Name);
                        break;

                }

                crystalReport.SetParameterValue("AppoinmentDate", reporting.AppointmentDateTime.Value.ToShortDateString());
                crystalReport.SetParameterValue("CollectionDate", reporting.CollectionDateTime.Value.ToShortTimeString());

                crystalReport.SetParameterValue("HospitalFee", reporting.Receipt.HospitalFee.Value);
                crystalReport.SetParameterValue("DoctorFee", reporting.Receipt.DoctorFee.Value);
                crystalReport.SetParameterValue("UrgentFee", reporting.Receipt.UrgentFee.Value);
                crystalReport.SetParameterValue("TotalReceiptAmount", reporting.Receipt.TotalAmount.Value);

                crystalReport.SetParameterValue("ReceiptTypeName", reporting.Receipt.ServiceType.Name);
                crystalReport.SetParameterValue("User", reporting.User.UserName);
                //crystalReport.SetParameterValue("CustomerName", chanel.Patient.name);
                //crystalReport.SetParameterValue("ReportName", chanel.Doctor.name);
                return this.Print(crystalReport, noOfCopies);

            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Print(Data.Receipt receipt, ReceiptReportType receiptReportType, int noOfCopies = 1)
        {
            ReportClass crystalReport;          
            try
            {
                switch (receiptReportType)
                {
                    case ReceiptReportType.ChannelingReceiptHospitalReport:
                        crystalReport = new Report.CrystalReports.ChannelingReceiptHospitalReport();
                        return this.Print(crystalReport, receipt.Channelings.First());
                        break;

                    case ReceiptReportType.ChannelingReceiptDoctorReport:
                        crystalReport = new Report.CrystalReports.ChannelingReceiptDoctorReport();
                        return this.Print(crystalReport, receipt.Channelings.First());
                        break;

                    case ReceiptReportType.XRayReceiptHospitalReport:
                        crystalReport = new Report.CrystalReports.XRayReceiptReport();
                        return this.Print(crystalReport, receipt.Reportings.First());
                        break;

                    case ReceiptReportType.XRayReceiptDoctorReport:
                        crystalReport = new Report.CrystalReports.XRayReceiptReport();
                        return this.Print(crystalReport, receipt.Reportings.First());
                        break;

                    case ReceiptReportType.LabTestReceiptHospitalReport:
                        crystalReport = new Report.CrystalReports.LabTestReceiptHospitalReport();
                        return this.Print(crystalReport, receipt.Reportings.First());
                        break;

                    case ReceiptReportType.LabTestReceiptDoctorReport:
                        crystalReport = new Report.CrystalReports.ScanReceiptDoctorReport();
                        return this.Print(crystalReport, receipt.Reportings.First());
                        break;

                    case ReceiptReportType.ScanReceiptHospitalReport:
                        crystalReport = new Report.CrystalReports.ScanReceiptHospitalReport();
                        return this.Print(crystalReport, receipt.Reportings.First());
                        break;

                    case ReceiptReportType.ScanReceiptDoctorReport:
                        crystalReport = new Report.CrystalReports.ScanReceiptDoctorReport();
                        return this.Print(crystalReport, receipt.Reportings.First());
                        break;

                    default:
                        return false;

                  
                }

            }
            catch (Exception)
            {

                throw;
            }
        }


        public bool Print(ReportClass crystalReport,int noOfCopies=1)
        {
            try
            {
                crystalReport.PrintOptions.PrinterName = this.GetDefaultPrinterName();
                crystalReport.PrintToPrinter(1, false, 0, 0);
                return true;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
